<template>
  <div class="search-popup">
    <form action="/">
      <van-search
        v-model="searchVal"
        show-action
        :placeholder="placeholderVal"
        @search="onSearch"
        @cancel="onCancel"
        @input="onInput"
      />
    </form>
    <HistoryHot
      v-if="blockShow == 1"
      :historyListData="historyListData"
      :hotKeywordListData="hotKeywordListData"
      @tagClick="tagClick"
    ></HistoryHot>

    <SearchTipsList
      v-else-if="blockShow == 2"
      :searchTipsListData="searchTipsListData"
      @cellClick="tagClick"
    ></SearchTipsList>

    <SearchProducts
      v-else
      :filterCategory="filterCategory"
      :goodsList="goodsList"
      @categoryChange="categoryChange"
      @priceChange="priceChange"
    ></SearchProducts>
  </div>
</template>

<script>
import HistoryHot from "../components/HistoryHot.vue";
import SearchTipsList from "../components/SearchTipsList.vue";
import SearchProducts from "../components/SearchProducts.vue";

import {
  GetSearchPopupData,
  GetSearchTipsListData,
  GetGoodsListData,
} from "../request/api";

import { Toast } from "vant";
export default {
  name: "searchPopup",
  data() {
    return {
      blockShow: 1,
      searchVal: "",
      placeholderVal: "请输入搜索关键词",
      // 历史记录的列表数据
      historyListData: [],

      // 热门搜索列表数据
      hotKeywordListData: [],

      // 搜索实时提示的列表数据
      searchTipsListData: [],

      // 搜索产品内容的分类数据
      filterCategory: [],

      // 搜索产品内容的列表数据
      goodsList: [],

      // 价格排序：(由高到低或者由低到高)
      order: "desc",

      // 分类id
      categoryId: 0,

      // 搜索方式  是id 还是price
      sort: "id",
    };
  },
  components: { HistoryHot, SearchTipsList, SearchProducts },
  methods: {
    onSearch(val) {
      this.blockShow = 3;
      GetGoodsListData({
        keyword: val,
        page: 1,
        size: 10,
        order: this.order,
        categoryId: this.categoryId,
        sort: this.sort,
      }).then((res) => {
        if (res.errno == 0) {
          let { filterCategory, goodsList } = res.data;

          this.goodsList = goodsList;

          let newArr = JSON.parse(
            JSON.stringify(filterCategory)
              .replace(/name/g, "text")
              .replace(/id/g, "value")
          );
          // console.log(newArr);
          this.filterCategory = newArr;
        }
      });
    },
    onCancel() {
      // 返回到上一路由
      this.$router.back(1);
      //   Toast("取消");
    },
    onInput(val) {
      this.blockShow = 2;
      // console.log(val);
      // 发送请求，获取实时搜索的文本提示的数据列表
      GetSearchTipsListData({ keyword: val }).then((res) => {
        if (res.errno == 0) {
          this.searchTipsListData = res.data;
          // console.log(res.data);
        }
      });
    },

    // 点击分类中选项发起搜索请求（子传父）
    categoryChange(val) {
      this.categoryId = val;
      this.onSearch(this.searchVal);
    },

    priceChange(val) {
      this.order = val;
      this.sort = "price";
      this.onSearch(this.searchVal);
    },

    // 点击历史记录和热门搜素标签进行搜索
    tagClick(val) {
      console.log(val);
      this.searchVal = val;
      this.onSearch(this.searchVal);
    },
  },
  created() {
    GetSearchPopupData().then((res) => {
      if (res.errno == 0) {
        this.placeholderVal = res.data.defaultKeyword.keyword;
        this.historyListData = res.data.historyKeywordList;
        this.hotKeywordListData = res.data.hotKeywordList;
      }
    });
  },
};
</script>

<style>
.search-popup {
  width: 100%;
  height: 100%;
  position: absolute;
  right: 0;
  top: 0;
  /* background-color: rgba(0, 0, 0, 0.8); */
  background-color: #efefef;
}
</style>